import { message } from 'antd';
import { extend } from 'umi-request';
//给每个请求都添加/api => 进行代理
const reqeust = extend({
  credentials: 'include',
  prefix: '/api',
  timeout: 1000,
});

reqeust.interceptors.request.use((url, options): any => {
  console.log(`do request url =>${url}`);

  return {
    url:
      process.env.NODE_ENV === 'production'
        ? 'http://192.168.140.128:8080' + url
        : 'http://localhost:8080' + url,
    options: {
      ...options,
      headers: {},
    },
  };
});

reqeust.interceptors.response.use(async (response): Promise<any> => {
  const res = await response.clone().json();
  if (res.code !== 0) {
    message.error(res.description);
  }
  return res;
});

export default reqeust;
